﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Shopping_Cart
/// </summary>
/// 
public class cartItem
{

    private string id;

    #region CartItem Get:Set

    private string produt_ID;

    public string Produt_ID
    {
        get { return produt_ID; }
        set { produt_ID = value; }
    }

    private string product_Code;

    public string Product_Code
    {
        get { return product_Code; }
        set { product_Code = value; }
    }

    private string product_Name;

    public string Product_Name
    {
        get { return product_Name; }
        set { product_Name = value; }
    }
    private double product_Price;

    public double Product_Price
    {
        get { return product_Price; }
        set { product_Price = value; }
    }

    private int quantity;

    public int Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }

    private float point_Value;

    public float Point_Value
    {
        get { return point_Value; }
        set { point_Value = value; }
    }

    private float business_value;

    public float Business_value
    {
        get { return business_value; }
        set { business_value = value; }
    }

    private string url;

    public string Url
    {
        get { return url; }
        set { url = value; }
    }

    private double tot_Price;

    public double Tot_Price
    {
        get { return product_Price * quantity; }
    }

    private double tot_Point_Value;
   

    public double Tot_Point_Value
    {
        get { return point_Value * quantity; }

    }

    private double tot_Business_Value;

    public double Tot_Business_Value
    {
        get { return business_value * quantity; }
       
    }


    public cartItem(string id,string code)
    {
        // TODO: Complete member initialization
        this.id = id;
        this.Product_Code = code;
    }


    public cartItem()
    {
        //'Default 
    }
    #endregion



}
    public class Shopping_Cart
    {

    #region Create Cart ItemList

    List<cartItem> items;
    public List<cartItem> Items
    {
        get { return items; }
        set { items = value; }
    }

    #endregion

    #region Shopping CartList

    public Shopping_Cart()
	{
        items = new List<cartItem>();
	}

    #endregion

    #region Find Correct Index

    public int Findindex(string Itemid,string code)
    {
        int index = -1;
        foreach (cartItem c in items)
        {
            index++;
            if (c.Produt_ID.Equals(Itemid) && c.Product_Code.Equals(code))
            {

                return index;
            }

        }

        return -1;

    }

    #endregion 



    #region Insert Shopping Cart Item 

    public void insertItems(string id, string code, string name, double pri, float pointv, float busi_val, int qun, string url)
    {
        int index = Findindex(id,code);

        if (index != -1)
        {
            items[index].Quantity += qun;

        }
        else
        {
            cartItem c = new cartItem();
            c.Produt_ID = id;
            c.Product_Code = code;
            c.Product_Name = name;
            c.Product_Price = pri;
            c.Point_Value = pointv;
            c.Business_value = busi_val;
            c.Quantity = qun;
            c.Url = url;
            items.Add(c);

        }


    }

    #endregion

    #region Total Calculate

    public double total
    {
        get
        {
            double tot = 0;
            foreach (cartItem c in items)
            {
                tot = tot + c.Tot_Price;
            }
            return tot;
        }


    }

    #endregion


    #region Total Point_value

    public double totalPoint_value
    {
        get
        {
            double totpv = 0;
            foreach (cartItem c in items)
            {
                totpv = totpv + c.Tot_Point_Value;
            }
            return totpv;
        }


    }

    #endregion



    #region Total Business_Value

    public double totalBusiness_Value
    {
        get
        {
            double totbv = 0;
            foreach (cartItem c in items)
            {
                totbv = totbv + c.Tot_Business_Value;
            }
            return totbv;
        }


    }

    #endregion



    #region Update  Cart 

    public void updateItems(string id, string code, string name, double pri, float pointv, float busi_val, int qun)
    {
        int index = Findindex(id, code);

        if (index != -1)
        {
            items[index].Quantity = qun;
            cartItem c = new cartItem();
            c.Produt_ID = id;
            c.Product_Code = code;
            c.Product_Name = name;
            c.Product_Price = pri;
            c.Point_Value = pointv;
            c.Business_value = busi_val;
            c.Quantity = qun;
            items.Add(c);
            items.RemoveAt(index);
        }
    }


        #endregion



    }